$(function () {
    obj.init();
});
var obj = new function () {
    return {
        id:null,
        form: null,
        formObj: null,
        init: function () {
            this.id = xhTool.getQuery('id');
            this.formObj = $('#xh_form');
            this.form = new xhForm(this.formObj);
            this.funObj().addSelectCss();
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#deviceType').on('change',function () {//修改类型
                $this.funObj().changeType();
            });
            $('#bt_next1').click(function () {//第一个下一步
                $this.funObj().doNext1();
            });
            $('#bt_next2').click(function () {//第二个下一步
                $this.funObj().doNext2();
            });
            $('#bt_next3').click(function () {//确定操作
                $this.funObj().saveCreate();
            });
            $('#bt_cancel1, #bt_cancel2, #bt_cancel3').click(function () {//取消操作
                window.top.xhTop.closeModal();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                addSelectCss: function () {
                    $("#deviceType").select2({minimumResultsForSearch: -1, width:200});
                },
                changeType: function () {
                    var deviceType = $('#deviceType').val();
                    if (deviceType === "0") {
                        $("#step1, #step2").hide();
                        $("#step3").show();
                    } else {
                        $("#step2, #step3").hide();
                        $("#step1").show();
                    }
                },
                doNext1: function () {
                    $("#type_msg").html('选择学校');
                    var deviceType = $('#deviceType').val();
                    if (deviceType === "3") {
                        $("#typeTr,#userTr, #step1, #step3").hide();
                        document.getElementById('step2').style.display = 'block';
                    } else {
                        $("#typeTr,#userTr, #step1, #step2").hide();
                        document.getElementById('step3').style.display = 'block';
                    }
                    document.getElementById('schoolTr').style.display = 'block';
                    xhExtra.ajaxRequest('device/getAllSchool', {}, {check:true}, function (data) {
                        $('#school').select2({placeholder: '请选择学校', data: data, allowClear: false, width:200});
                    });
                },
                doNext2: function () {
                    var school = $('#school').val();
                    if (!school) {
                        window.top.xhTop.showError('请选择学校');
                        return false;
                    }
                    $("#type_msg").html('选择老师');
                    $("#typeTr,#schoolTr, #step1, #step2").hide();
                    document.getElementById('userTr').style.display = 'block';
                    document.getElementById('step3').style.display = 'block';
                    xhExtra.ajaxRequest('device/getTchBySchool', {school: school}, {check:true}, function (data) {
                        $('#teacher').select2({placeholder: '请选择老师', data: data, allowClear: false, width:200});
                    });
                },
                saveCreate: function () {
                    var deviceType = $('#deviceType').val();
                    var school = $('#school').val();
                    var teacher = $('#teacher').val();
                    var params = {id: $this.id};
                    if (!$this.id) {
                        window.top.xhTop.showError('缺少id参数');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    params.deviceType = deviceType;
                    if (deviceType === '3') {
                        if (!school || !teacher) {
                            window.top.xhTop.showError('请完整选择学校和老师');
                            return false;
                        }
                        params.school = school;
                        params.teacher = teacher;
                    } else if (deviceType !== '0') {
                        if (!school) {
                            window.top.xhTop.showError('请选择学校');
                            return false;
                        }
                        params.school = school;
                    }
                    xhExtra.ajaxRequest('device/saveExchangeDevice', params, {success: true,check:false},function (res) {
                        if(res.code === 200) {
                            window.top.swal('成功',res.msg,'success');
                            window.top.xhTop.closeModal();
                        } else {
                            window.top.xhTop.showError(res.error);
                        }
                    });
                }
            }
        }
    }
};